package com.example.apiproxydemo.core.filter;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j2;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;

/**
 * @Description TODO
 * @Author zhoupan
 * @Date 2022/2/21 11:11
 */
@Log4j2
public abstract class 过滤器抽象类 implements WebFilter {
    final String test管理接口 = "_manage";
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        //获取请求地址
        var test = exchange.getRequest().getHeaders().get(test管理接口);
        if(CollUtil.isNotEmpty(test)||"/test/api/list".equals( exchange.getRequest().getPath().value()))
        {
            // 解析exchange,返回一个全新的mono（缓存body的mono）
//            Mono<Void> monoData = DataBufferUtils.join(exchange.getRequest().getBody()).flatMap(dataBuffer -> {
//                // 创建一个容量为dataBuffer容量大小的字节数组
//                byte[] bytes = new byte[dataBuffer.readableByteCount()];
//                // dataBuffer类容读取到bytes中
//                dataBuffer.read(bytes);
//                // 释放缓冲区
//                DataBufferUtils.release(dataBuffer);
//                // 创建新缓冲区并写入数据
//                Flux<DataBuffer> cachedFlux = Flux.defer(() -> {
//                    DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes);
//                    DataBufferUtils.retain(buffer);
//                    return Mono.just(buffer);
//                });
//                // 由于原来的request请求参数被消费，需要提供新的请求
//                ServerHttpRequest mutatedRequest = new ServerHttpRequestDecorator(exchange.getRequest()) {
//                    @Override
//                    public Flux<DataBuffer> getBody() {
//                        return cachedFlux;
//                    }
//                };
//                // 创建新的exchange并构建解析的数据
//                ServerWebExchange mutatedExchange = exchange.mutate().request(mutatedRequest).build();
//                Mono<Void> mono = ServerRequest.create(mutatedExchange, new ArrayList<>())
//                        .bodyToMono(String.class)
//                        .doOnNext(objectValue -> {
//                            log.info("resolve success postBody is:{}", objectValue);
////                            gatewayContext.setCacheBody(objectValue);
//                        }).then(chain.filter(mutatedExchange));
//                return mono;
//            });
            log.info  ("测试接口:{}",exchange.getRequest().getPath().value());
            return chain.filter(exchange);
        }
        return doFilter(exchange,chain);
    }
    abstract Mono<Void> doFilter(ServerWebExchange exchange, WebFilterChain chain);

}
